package com.youlai.boot.factory.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.factory.model.form.CustomersForm;
import com.youlai.boot.factory.model.query.CustomersQuery;
import com.youlai.boot.factory.model.vo.CustomersVO;
import com.youlai.boot.factory.service.CustomersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 客户前端控制层
 *
 * @author youlaitech
 * @since 2025-07-06 00:42
 */
@Tag(name = "客户接口")
@RestController
@RequestMapping("/api/v1/customers")
@RequiredArgsConstructor
public class CustomersController  {

    private final CustomersService customersService;

    @Operation(summary = "客户分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:customers:query')")
    public PageResult<CustomersVO> getCustomersPage(CustomersQuery queryParams) {
        IPage<CustomersVO> result = customersService.getCustomersPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增客户")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('factory:customers:add')")
    public Result<Void> saveCustomers(@RequestBody @Valid CustomersForm formData ) {
        boolean result = customersService.saveCustomers(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取客户表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:customers:edit')")
    public Result<CustomersForm> getCustomersForm(
        @Parameter(description = "客户ID") @PathVariable Long id
    ) {
        CustomersForm formData = customersService.getCustomersFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改客户")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:customers:edit')")
    public Result<Void> updateCustomers(
            @Parameter(description = "客户ID") @PathVariable Long id,
            @RequestBody @Validated CustomersForm formData
    ) {
        boolean result = customersService.updateCustomers(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除客户")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:customers:delete')")
    public Result<Void> deleteCustomerss(
        @Parameter(description = "客户ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = customersService.deleteCustomerss(ids);
        return Result.judge(result);
    }
}
